package eu.ricordo.reasoner.deamon;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.owllink.OWLlinkHTTPXMLReasonerFactory;
import org.semanticweb.owlapi.owllink.OWLlinkReasoner;
import org.semanticweb.owlapi.owllink.OWLlinkReasonerConfiguration;
import org.semanticweb.owlapi.owllink.OWLlinkReasonerIOException;
import org.semanticweb.owlapi.owllink.builtin.requests.CreateKB;
import org.semanticweb.owlapi.owllink.builtin.requests.LoadOntologies;
import org.semanticweb.owlapi.owllink.builtin.response.KB;
import org.semanticweb.owlapi.owllink.builtin.response.OK;
import org.semanticweb.owlapi.owllink.builtin.response.OWLlinkErrorResponseException;
import org.semanticweb.owlapi.owllink.server.OWLlinkServer;
import org.semanticweb.owlapi.owllink.server.serverfactory.PelletServerFactory;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Sarala Wimalaratne
 *         Date: 12-Aug-2011
 *         Time: 08:06:06
 */

public class KBCreator {
    public final String DOCUMENT_IRI = "/home/sarala/ricordoeu/ricordows/resources/pato.owl";
    private OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

    private OWLOntology ontology =null;//Create an OWLOntology;
    private OWLlinkReasoner reasoner =null;
    private String ou = "http://www.ricordo.eu/ricordo.owl";
    private IRI docIRI;
    private OWLlinkServer server = null;
    private String port = "8081";
    private Logger logger = Logger.getLogger("eu.ricordo.reasoner.deamon.kbcreator");

    public void setUpReasoner(){
        logger.info("Setting up reasoner");
        docIRI=IRI.create(new File(DOCUMENT_IRI));

        try {
            ontology = manager.createOntology();
        } catch (OWLOntologyCreationException e) {
            e.printStackTrace();
        }

        URL url = null;//Configure the server end-point
        try {
             //url = new URL("http://bioonto.gen.cam.ac.uk:"+port);
            url = new URL("http://localhost:"+port);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        OWLlinkReasonerConfiguration reasonerConfiguration = new OWLlinkReasonerConfiguration(url);
        OWLlinkHTTPXMLReasonerFactory factory = new OWLlinkHTTPXMLReasonerFactory();
        try{
            reasoner = factory.createNonBufferingReasoner(ontology, reasonerConfiguration);
        }catch (OWLlinkReasonerIOException e){
            System.out.println(e.getMessage());
        }

    }

    public void createKB(){
        logger.info("Creating KB");
        try {
            long st = System.currentTimeMillis();
            CreateKB createKBRequest = new CreateKB(IRI.create(ou));
            KB kbResponse = reasoner.answer(createKBRequest);
            LoadOntologies loadOntologies = new LoadOntologies(kbResponse.getKB(),docIRI);
            OK okResponse = reasoner.answer(loadOntologies);
            logger.info("KB load time: "+(System.currentTimeMillis() - st));
        } catch (OWLlinkErrorResponseException e) {
            logger.log(Level.WARNING,"Problem creating KB",e);
        }
    }

    public void startPelletServer(){
        logger.info("Starting pellet server");
        PelletServerFactory pellet = new PelletServerFactory();
        server = pellet.createServer(Integer.parseInt(port));
        server.run();
    }

    public void stopPelletServer(){
        logger.info("Stopping pellet server");
        if(server!=null) {
            try {
                server.stop();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
